# -- coding: utf-8 --
import os
import sys
import tensorflow as tf
import argparse
from tensorflow import gfile    #用于多平台文件路径格式兼容
from six.moves import xrange  
import cv2
import numpy as np
from openslide import open_slide, OpenSlideUnsupportedFormatError
import re
import pdb
import random
from PIL import Image, ImageDraw
FLAGS = tf.app.flags.FLAGS
user = 'guobin'
project ='prostate'
#文件类型
GLOB_WSI_TYPE_PATTEN = '*.ndpi'
GLOB_XML_TYPE_PATTEN = '*.xml'
GLOB_MASK_TYPE_PATTEN = '*.tif'
#样本命名前缀
PATCH_NORMAL_PREFIX = 'normal_'
PATCH_TUMOR_PREFIX = 'tumor_'


# ruing tensorflow with GPU = 0
os.environ['CUDA_VISIBLE_DEVICES'] = '0'

#----------------------------公共部分------------------------------------
# 内容:训练集WSI、测试集WSI、GroudTruth（XML、MASK(需要生成)）
#根目录
DATASET_DIR = '/home/ramsley/DataSet/prostate/1-4/'
PUBLIC_TRAIN_DATASET = DATASET_DIR + 'trainingData/'
PUBLIC_VALIDATION_DATASET = DATASET_DIR + 'testingData/'

#训练集WSI、GroundTruth
TRAIN_TUMOR_WSI_DIR = PUBLIC_TRAIN_DATASET + 'Train_Tumor/'
TRAIN_NORMAL_WSI_DIR = PUBLIC_TRAIN_DATASET + 'Train_Normal/'
TRAIN_TUMOR_MASK_DIR = PUBLIC_TRAIN_DATASET + 'groundTruth/mask/'
TRAIN_TUMOR_XML_DIR = PUBLIC_TRAIN_DATASET + 'groundTruth/xml/'

#测试集WSI、GroundTruth
VALIDATION_WSI_DIR = PUBLIC_VALIDATION_DATASET + 'TestSet/'
VALIDATION_TUMOR_MASK_DIR = PUBLIC_VALIDATION_DATASET + 'groundTruth/mask/'
VALIDATION_TUMOR_XML_DIR = PUBLIC_VALIDATION_DATASET + 'groundTruth/xml/'

#checkpoint存储路径
PUBLIC_PRETRAIN_CKPT_DIR = '/home/ramsley/workspace/pretrained_ckpt/'
INCEPTION_V3_CKPT = PUBLIC_PRETRAIN_CKPT_DIR + 'inception_v3'
INCEPTION_V4_CKPT = PUBLIC_PRETRAIN_CKPT_DIR + 'inception_v4'
INCEPTION_RESNET_V2_CKPT = PUBLIC_PRETRAIN_CKPT_DIR + 'inception_resnet_v2'#/inception_resnet_v2.ckpt'
RESNET_V2_50_CKPT = PUBLIC_PRETRAIN_CKPT_DIR + 'resnet_v2_50'
RESNET_V2_101_CKPT = PUBLIC_PRETRAIN_CKPT_DIR + 'resnet_v2_101'
RESNET_V2_152_CKPT = PUBLIC_PRETRAIN_CKPT_DIR + 'resnet_v2_152'
VGG_16_CKPT = PUBLIC_PRETRAIN_CKPT_DIR + 'vgg_16'
VGG_19_CKPT = PUBLIC_PRETRAIN_CKPT_DIR + 'vgg_19'



#----------------------------用户部分------------------------------------内容：patch、label-txt、OverviewRef、tf-records

#个人数据目录（自定义预处理）；
USR_DATASET_DIR = '/home/'+ user + '/' + project +'/'

USR_TRAIN_DATASET = USR_DATASET_DIR + 'trainingData/'
USR_VALIDATION_DATASET = USR_DATASET_DIR + 'testingData/'

#训练集：分类patches、txt、tfrecords
PROSTATE_PATCHES_TRAIN = USR_TRAIN_DATASET + 'patch-based-classification/'							
PROSTATE_PATCHES_TRAIN_TUMOR_NEG = PROSTATE_PATCHES_TRAIN + 'tumor-label-0/'					#Tumor WSI非标注区（label = 0）
PROSTATE_PATCHES_TRAIN_NORMAL_NEG = PROSTATE_PATCHES_TRAIN + 'label-0/'
PROSTATE_PATCHES_TRAIN_TUMOR_POS = PROSTATE_PATCHES_TRAIN + 'label-1/'
PROSTATE_PATCHES_TRAIN_TXT = PROSTATE_PATCHES_TRAIN + 'label-txt/'
TRAIN_TFRECORDS_DIR = PROSTATE_PATCHES_TRAIN + 'tf-records/'
TRAIN_TISSUE_CONTOUR_DIR = PROSTATE_PATCHES_TRAIN + 'OverviewRef/ov_image/'						#带轮廓的缩略图
TRAIN_PATCH_DISTRIBUTION_DIR = PROSTATE_PATCHES_TRAIN + 'OverviewRef/patch_distribution_image/' #取样标记图

#测试集：分类patches、txt、tfrecords
PROSTATE_PATCHES_VALIDATION = USR_VALIDATION_DATASET + 'patch-based-classification/'
PROSTATE_PATCHES_VALIDATION_NEG = PROSTATE_PATCHES_VALIDATION + 'label-0/'
PROSTATE_PATCHES_VALIDATION_POS = PROSTATE_PATCHES_VALIDATION + 'label-1/'
PROSTATE_PATCHES_VALIDATION_TXT = PROSTATE_PATCHES_VALIDATION + 'label-txt/'
VALIDATION_TFRECORDS_DIR = PROSTATE_PATCHES_VALIDATION + 'tf-records/'
VALIDATION_TISSUE_CONTOUR_DIR = PROSTATE_PATCHES_VALIDATION + 'OverviewRef/ov_image/'
VALIDATION_PATCH_DISTRIBUTION_DIR = PROSTATE_PATCHES_VALIDATION + 'OverviewRef/patch_distribution_image/'


#以下全局变量可通过命令行输入
tf.app.flags.DEFINE_string('xmlDir', TRAIN_TUMOR_XML_DIR, '''''')
tf.app.flags.DEFINE_string('slideDir', TRAIN_TUMOR_WSI_DIR, '''''')
tf.app.flags.DEFINE_string('maskDir', TRAIN_TUMOR_MASK_DIR, '''''')
tf.app.flags.DEFINE_string('output_NEG_patchDir', PROSTATE_PATCHES_TRAIN_NORMAL_NEG, '''''')
tf.app.flags.DEFINE_string('output_POS_patchDir', PROSTATE_PATCHES_TRAIN_TUMOR_NEG, '''''')
tf.app.flags.DEFINE_string('output_txtDir', PROSTATE_PATCHES_TRAIN_TXT, '''''')


if __name__ == '__main__':
    private_directorylist=[USR_DATASET_DIR,USR_TRAIN_DATASET,USR_VALIDATION_DATASET,PROSTATE_PATCHES_TRAIN,PROSTATE_PATCHES_TRAIN_TUMOR_NEG,PROSTATE_PATCHES_TRAIN_NORMAL_NEG,PROSTATE_PATCHES_TRAIN_TUMOR_POS,PROSTATE_PATCHES_TRAIN_TXT,VALIDATION_TFRECORDS_DIR,VALIDATION_TISSUE_CONTOUR_DIR,VALIDATION_TISSUE_CONTOUR_DIR,VALIDATION_PATCH_DISTRIBUTION_DIR]
    for directory in private_directorylist:
        if not os.path.exists(directory):
            os.makedirs(directory)
    print ("successfully create private work directories~")